package com.ys.everydaynews.view

import android.app.Activity
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.ys.everydaynews.R

class SettingsItemView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {

    private var leftTitle: String? = ""
    private var rightTitle: String? = ""
    private var contentTitle: String? = ""
    private var isShowBtn: Boolean = false
    private var isShowImg: Boolean = false
    private val setingsArray: TypedArray =
        context.obtainStyledAttributes(attrs, R.styleable.SettingsItem)
    private var view: View? = null

    init {
        leftTitle = setingsArray.getString(R.styleable.SettingsItem_leftTitle)
        rightTitle = setingsArray.getString(R.styleable.SettingsItem_rightTitle)
        contentTitle = setingsArray.getString(R.styleable.SettingsItem_contentTitle)
        isShowBtn = setingsArray.getBoolean(R.styleable.SettingsItem_isShowBtn, false)
        isShowImg = setingsArray.getBoolean(R.styleable.SettingsItem_isShowImg, false)
        setingsArray.recycle()
        initView()
    }

    private fun initView() {
        view = LayoutInflater.from(context).inflate(R.layout.item_setings, this)
        val left_title: TextView = view!!.findViewById(R.id.left_title)
        val right_title: TextView = view!!.findViewById(R.id.right_title)
        val left_content: TextView = view!!.findViewById(R.id.left_content)
        left_title.text = leftTitle
        right_title.text = rightTitle
        left_content.text = contentTitle
        isShowBtn(view!!)
        isShowImg(view!!)
    }

    private fun isShowBtn(view: View) {
        val btn_off: Switch = view.findViewById(R.id.btn_off)
        if (isShowBtn) {
            btn_off.visibility = View.VISIBLE
        } else {
            btn_off.visibility = View.GONE
        }
    }

    private fun isShowImg(view: View) {
        val iv_right: ImageView = view.findViewById(R.id.iv_right)
        if (isShowImg) {
            iv_right.visibility = View.VISIBLE
        } else {
            iv_right.visibility = View.GONE
        }
    }
    fun setBgSetItem(colorId:Int){
        val rl_item:RelativeLayout=view!!.findViewById(R.id.rl_item)
        rl_item.setBackgroundColor(ContextCompat.getColor(context,colorId))
    }
}